// pages/parenting/parenting.js
const util = require("../../utils/util")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgPath: "/images",
    isShow:false,
    eheight:'',
    eweight:'',
    getNews:[],
    home:null,
    badyCid:[
      {
        id:1,
        title:'0-2',
        icon:'http://qnstatic.lianhequnmei.com/images/20221220/icons_b1.png',
      },
      {
        id:2,
        title:'2-4',
        icon:'http://qnstatic.lianhequnmei.com/images/20221220/icons_b2.png',  
      },
      {
        id:3,
        title:'4-6',
        icon:'http://qnstatic.lianhequnmei.com/images/20221220/icons_b4.png',  
      }
    ],
    badyData:{
      '0-2':{
        id:1,
        title:'0-2月龄家庭育儿建议',
        content:[
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
          content:'宝宝保护肚脐很重要，要时刻关注'
        },
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/two.png',
          content:'1211宝宝保护肚脐很重要，要时刻关注'
        }
        ],
      },
      '2-4':{
        id:2,
        title:'2-4月龄家庭育儿建议',
        content:[
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
          content:'宝宝保护肚脐很重要，要时刻关注'
        },
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/two.png',
          content:'111宝宝保护肚脐很重要，要时刻关注'
        },
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/three.png',
          content:'111宝宝保护肚脐很重要，要时刻关注'
        },
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
          content:'111宝宝保护肚脐很重要，要时刻关注'
        }
        ],
      },
      '4-6':{
        id:2,
        title:'4-6月龄家庭育儿建议',
        content:[
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
          content:'宝宝保护肚脐很重要，要时刻关注'
        },
        {
          num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
          content:'111宝宝保护肚脐很重要，要时刻关注'
        }
        ],
      }
    },
    badyDataitem:{
      id:1,
      title:'0-2月龄家庭育儿建议',
      content:[
      {
        num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/one.png',
        content:'宝宝保护肚脐很重要，要时刻关注'
      },
      {
        num_icon:'http://qnstatic.lianhequnmei.com/images/20221220/two.png',
        content:'111宝宝保护肚脐很重要，要时刻关注'
      }
      ],
    },
    bady_bottom:'310',
    bothAge:'0岁0月00天',
    article_collection_total:0,
    checked_s:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    const api = require("../../api/api");
    this.setData({
      imgPath: util.getImgPath(),
    })
    api.news.getList({
      page:1,
      page_size:15,
      article_category_id:0,
      title:'',
      success(res){
          if(res.data.code == 0){
             for(let i in res.data.data.rows){
              that.data.getNews.push({
                image:res.data.data.rows[i].image,
                title:res.data.data.rows[i].title,
                tags:res.data.data.rows[i].tags,
                view_number:res.data.data.rows[i].view_number,
                article_category_id:res.data.data.rows[i].article_category_id,
                id:res.data.data.rows[i].id
               });
             }
          that.setData({
            getNews:that.data.getNews
          });
          console.log(that.data.getNews);
          }else{
              wx.showToast({
                title: res.data.msg,
                 icon:'none',
                 duration: 5000,
              })
          }
      }
    })
  },
  tabcontent(e){
     let title = e.currentTarget.dataset.title;
     let list =  this.data.badyData;
     this.setData({
      badyDataitem: title,
      bady_bottom:310,
      checked_s:true
     });  
  },
  save_msg(){
    const api = require("../../api/api");
    let that = this;
    api.news.updateBaby({
      height:that.data.eheight,
      weight:that.data.eweight,
      success(res){
        if(res.data.code == 0){
          wx.showToast({
            icon:'none',
            title:'保存成功'
          });  
          that.setData({
            isShow:false
           });
        }
      }

    });  
  },
  moreData(){
      let badyDataitems =  this.data.badyDataitem;
      let num = 0;
      
        if(badyDataitems.content.length>3){
     
          if(this.data.checked_s){
            num = 310 + (badyDataitems.content.length - 3) * 70;
            this.setData({
              bady_bottom:num,
              checked_s:false
            });    
          }else{
            this.setData({
              bady_bottom:310,
              checked_s:true
            });
          }
        }else{
          this.setData({
            bady_bottom:310,
            checked_s:false
          });  
        }

  },
  jumpViewdet(e){
    let aid = e.currentTarget.dataset.id;
    let typeid = e.currentTarget.dataset.article_category_id;
    let url = `/pages/parenting/article?aid=${aid}&typeid=${typeid}`
     wx.navigateTo({
       url: url
     })
  },
  jumpList(){
    wx.navigateTo({
      url: '/pages/parenting/classify',
    })
  },
  gocollect(){
    wx.navigateTo({
      url: '/pages/parenting/collect',
    })
  },
  pop_weight(){
     this.setData({
      isShow:true
     });
  },
  onSpecPopupClose() {
    this.setData({
      isShow: false,
    })
  },
  mymethod(birthday){ //根据日期算年龄
    if(birthday){
      birthday=birthday.split('-');
      let date = new Date();
      let today = [date.getFullYear(), date.getMonth() + 1, date.getDate()];
      let age = today.map((val, index) => {
          return val - birthday[index]
      })
      if (age[2] < 0) {
          let lastMonth = new Date(today[0], today[1], 0)
          age[1]--
          age[2] += lastMonth.getDate()
      }
      if (age[1] < 0) {
          age[0]--
          age[1] += 12
      }
      return age[0]+'岁'+age[1]+'月'+age[2]+'天'
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

    const api = require("../../api/api");
    let that = this;
    api.news.suggests({
        success(res){
        if(res.data.code == 0){
           that.setData({
            badyCid:res.data.data,
            badyDataitem:res.data.data[0],
           });
        }
        }
    });

    api.user.home({
      success(res) {
        that.setData({
          home: res.data.data,
          eheight:res.data.data.baby_info.height,
          eweight:res.data.data.baby_info.weight,
          bothAge:that.mymethod(res.data.data.baby_info.birthday),
          article_collection_total:res.data.data.article_collection_total
        })
        //保存home到缓存
        wx.setStorageSync('home', res.data.data);
      }
    })





  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})